﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections;
using PowerShell.Host.PsTFS.CdmLet.PsObject;

namespace PowerShell.Host.PsTFS.CdmLet
{
    /// <summary>
    /// Récupére les fichier en attente d'archivage
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "PendingStatus")]
    public class Get_PendingStatus : Cmdlet
    {
        public Get_PendingStatus()
        {
            if (TFSProvider.s_TFSDriveInfo != null)
            {
                this.TFS = TFSProvider.s_TFSDriveInfo.TFS;
                this.ProjectName = TFSProvider.s_TFSDriveInfo.ProjectName;
            }
        }
        protected override void ProcessRecord()
        {
            if (TFS == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server TFS!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
            }
            List<PsPendingStatus> lPs = new List<PsPendingStatus>();

            // Get a reference to the version control service
            VersionControlServer versionControl = PsService.VCS(TFS);

            String path = string.Empty;

            if (ProjectName == null)
            {
                ProjectName = string.Empty;
            }

            if (ProjectName.Contains('$'))
            {
                path = ProjectName;
            }
            else
            {
                path = "$/" + ProjectName;
            }

            // Query for any pending changes for the specified path
            PendingSet[] sets = versionControl.QueryPendingSets(
                new[] { new ItemSpec(path, RecursionType.Full) }, null, null);


            if (sets != null)
            {
                foreach (PendingSet set in sets)
                {
                    PsPendingStatus pps = new PsPendingStatus();
                    pps.Computer = set.Computer;
                    pps.Name = set.Name;
                    pps.OwnerName = set.OwnerName;
                    
                    foreach (var item in set.PendingChanges)
                    {
                        PsFilePending pfp = new PsFilePending();
                        pfp.CreationDate = item.CreationDate;
                        pfp.FileName = item.FileName;
                        pfp.IsAdd = item.IsAdd;
                        pfp.IsBranch = item.IsBranch;
                        pfp.IsDelete = item.IsDelete;
                        pfp.IsEdit = item.IsEdit;
                        pfp.IsEncoding = item.IsEncoding;
                        pfp.IsLocalItemDelete = item.IsLocalItemDelete;
                        pfp.IsLock = item.IsLock;
                        pfp.IsMerge = item.IsMerge;
                        pfp.IsRename = item.IsRename;
                        pps.LFilePending.Add(pfp);
                    }

                    lPs.Add(pps);
                }
            }

            //Retourne la liste 
            WriteObject(lPs);
        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            PsLog.AddLog(null, "Add-PendingStatus is end");
            base.EndProcessing();
        }

        [Parameter(Position = 0, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public TeamFoundationServer TFS { get; set; }


        [Parameter(Position = 1)]
        [ValidateNotNullOrEmpty]
        public String ProjectName { get; set; }
    }

}